package com.xxkj.drugtraceability.ui.hascollected

import android.app.Application
import androidx.fragment.app.Fragment
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.xxhis.drugtraceability.base.BaseViewModel
import com.xxkj.drugtraceability.bean.DrugInStorageBean
import com.xxkj.drugtraceability.bean.DrugOutStorageBean
import com.xxkj.drugtraceability.bean.TwoItemData
import com.xxkj.drugtraceability.net.Result
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

/**
 * author: zhoujingjin
 * created on: 2024/9/18 15:53
 * description:
 */
class HasCollectedViewModel(application: Application,private val repository: HasCollectedRepository):BaseViewModel(application) {
    var tabList = mutableListOf("已采集入库药品","已采集处方药品","已采集医嘱药品")
    var mFragmentList = mutableListOf<Fragment>()
    var oneList = MutableLiveData<List<DrugInStorageBean>>()
    var oneOutList = MutableLiveData<List<DrugOutStorageBean>>()
    var twoList = MutableLiveData<List<TwoItemData>>()
    var threeList = MutableLiveData<List<TwoItemData>>()
    var dateTime = MutableLiveData<String>("")
    var hasMore = true
    var currentPosition = 0
    fun queryInStorageInfo(data:String){
        viewModelScope.launch(Dispatchers.Main) {
            withContext(Dispatchers.IO) {
               val result = repository.queryInStorageInfo(data)
                if (result is Result.Success){
                    hasMore = result.data.data.totalRecords>result.data.data.records.size
                    oneList.postValue(result.data.data.records)
                }else if (result is Result.Error){

                }
            }
        }
    }
    fun getDrugOutDepotList(data:String){
        viewModelScope.launch(Dispatchers.Main) {
            withContext(Dispatchers.IO) {
               val result = repository.getDrugOutDepotList(data)
                if (result is Result.Success){
                    hasMore = result.data.data.totalRecords>result.data.data.records.size
                    oneOutList.postValue(result.data.data.records)
                }else if (result is Result.Error){

                }
            }
        }
    }
    fun getTracCodeList(collectEndTime:String,
                        collectStartTim:String,
                        dataType:String,
                        patientId:String,
                        pdaSearchKeyWord:String,
                        psnName:String,
                        tradeType:String,
                        visitNo:String){
        viewModelScope.launch(Dispatchers.Main) {
            withContext(Dispatchers.IO) {
               val result = repository.tracCodeList(collectEndTime,collectStartTim,dataType,patientId,pdaSearchKeyWord,psnName,tradeType,visitNo)
                if (result is Result.Success){
                    if (dataType=="1")
                    twoList.postValue(result.data.rows)
                    else threeList.postValue(result.data.rows)
                }else if (result is Result.Error){

                }
            }
        }
    }
    fun getTracCodeList2(collectEndTime:String,
                        collectStartTim:String,
                        dataType:String,
                        patientId:String,
                        pdaSearchKeyWord:String,
                        psnName:String,
                        tradeType:String,
                        visitNo:String){
        viewModelScope.launch(Dispatchers.Main) {
            withContext(Dispatchers.IO) {
                val result = repository.tracCodeList(collectEndTime,collectStartTim,dataType,patientId,pdaSearchKeyWord,psnName,tradeType,visitNo)
                if (result is Result.Success){
                 threeList.postValue(result.data.rows)
                }else if (result is Result.Error){

                }
            }
        }
    }
}